package com.lt.suangua.ui.fragment;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lt.base.BaseAdapter;
import com.lt.suangua.R;
import com.lt.suangua.app.TitleBarFragment;
import com.lt.suangua.http.api.UserInfoApi;
import com.lt.suangua.http.glide.GlideApp;
import com.lt.suangua.http.model.HttpData;
import com.lt.suangua.ui.activity.BrowserActivity;
import com.lt.suangua.ui.activity.HomeActivity;
import com.lt.suangua.ui.activity.LoginActivity;
import com.lt.suangua.ui.adapter.MyItemAdapter;
import com.lt.suangua.utils.SPUtils;
import com.lt.suangua.utils.UserInfoUtils;
import com.lt.widget.layout.SettingBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import okhttp3.Call;

public class MyFragment extends TitleBarFragment<HomeActivity> {
    SettingBar setbar_service;
    AppCompatImageView iv_find_circle;
    AppCompatButton btn_logout;
    TextView tv_name, tv_desc, tv_read_num, tv_test_num, tv_login_num, tv_level;
    RecyclerView rv_my;
    RelativeLayout rl_login;
    MyItemAdapter myItemAdapter;
    SmartRefreshLayout refreshLayout;

    public static MyFragment newInstance() {
        return new MyFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView() {
        iv_find_circle = findViewById(R.id.iv_find_circle);
        tv_name = findViewById(R.id.tv_name);
        tv_desc = findViewById(R.id.tv_desc);
        tv_read_num = findViewById(R.id.tv_read_num);
        tv_test_num = findViewById(R.id.tv_test_num);
        tv_login_num = findViewById(R.id.tv_login_num);
        tv_level = findViewById(R.id.tv_level);
        btn_logout = findViewById(R.id.btn_logout);
        rv_my = findViewById(R.id.rv_my);
        refreshLayout = findViewById(R.id.refreshLayout);
        rl_login = findViewById(R.id.rl_login);
        setOnClickListener(R.id.btn_logout, R.id.rl_login);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                initData();
            }
        });

        if (!UserInfoUtils.isLogin(getActivity())) {
            tv_name.setText("你好，请登录");
        }


    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
/*        if (view.getId() == R.id.setbar_service) {
            XXPermissions.with(getActivity()).permission(Manifest.permission.CALL_PHONE).request(new OnPermissionCallback() {
                @Override
                public void onGranted(List<String> permissions, boolean all) {
                    if (all) {
                        // startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("13888888888")));
                        Intent callIntent = new Intent(Intent.ACTION_CALL);
                        callIntent.setData(Uri.parse("tel:+13888888888"));
                        startActivity(callIntent);
                    } else {
                        toast("请打开拨打电话权限");
                    }

                }
            });

        } else*/
        if (view.getId() == R.id.btn_logout) {
            logout();
            startActivity(new Intent(getActivity(), LoginActivity.class));
        } else if (view.getId() == R.id.rl_login) {//登录
            if (!UserInfoUtils.isLogin(getActivity())) {
                logout();
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        }
    }


    @Override
    public boolean isStatusBarEnabled() {
        // 使用沉浸式状态栏
        return !super.isStatusBarEnabled();
    }

    @Override
    public void initData() {
        EasyHttp.post(this)
                .api(new UserInfoApi().setToken(UserInfoUtils.getToken(getActivity())))
                .request(new HttpCallback<HttpData<UserInfoApi.Bean>>(this) {


                    @Override
                    public void onSucceed(HttpData<UserInfoApi.Bean> data) {

                        setData(data.getData());

                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                        Log.d("shuju", e.getMessage());
                        if ("未登录".equals(e.getMessage())) {
                            logout();
                        }
                    }

                    @Override
                    public void onEnd(Call call) {
                        super.onEnd(call);
                        refreshLayout.finishRefresh(300);
                    }
                });
    }

    public void setData(UserInfoApi.Bean bean) {
        // 显示圆形的 ImageView
        GlideApp.with(this)
                .load(bean.pic)
                .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                .into(iv_find_circle);
        tv_name.setText("你好," + bean.name);
        tv_desc.setText(bean.desc);
        tv_read_num.setText(bean.readnum);
        tv_test_num.setText(bean.testnum);
        tv_login_num.setText(bean.loginnum);
        tv_level.setText(bean.level);

        myItemAdapter = new MyItemAdapter(getActivity());
        myItemAdapter.setData(bean.list);
        myItemAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                Log.d("clickUrl2", myItemAdapter.getItem(position).url);
                if (!UserInfoUtils.isLogin(getActivity())) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                    return;
                }
                BrowserActivity.start(getActivity(), bean.list.get(position).url, bean.list.get(position).name);
            }
        });
        rv_my.setAdapter(myItemAdapter);

    }

    public void logout() {
        SPUtils.put(getActivity(), "token", "");
        tv_name.setText("你好，请登录");
        tv_desc.setText("欢迎来到易经测算");
        tv_read_num.setText("0");
        tv_test_num.setText("0");
        tv_login_num.setText("0");
        tv_level.setText("0");
    }

}
